package model

import (
	jsoniter "github.com/json-iterator/go"
	"time"
)

type AcceptUserVO struct {
	AdminUser  *User         `json:"user"`  // 用户本身的信息
	AddRoles   []*CasbinRule `json:"addRoles"`   // 增加的角色
	DeleteRids []int64       `json:"deleteRids"` // 需删除的角色
}

type User struct {
	ID           uint       `gorm:"primary_key"`
	CreatedAt    time.Time  `json:"createdTime"`
	UpdatedAt    time.Time  `json:"updatedTime"`
	UserName     string     `json:"userName" gorm:"NOT NULL;UNIQUE"`
	PassWord     string     `json:"passWord"`
	JobId        uint       `json:"jobId"`
	Job          Job        `gorm:"ForeignKey:JobId"`
	DepartmentId uint       `json:"departmentId"`
	Department   Department `gorm:"ForeignKey:DepartmentId"`
	Sex          bool       `json:"sex"`
	Email        string     `json:"email"`
	Phone        string     `json:"phone"`
	Enable       bool       `json:"enable"`
	Online       bool       `json:"online"`
}

type CustomUser struct {
	ID         uint             `json:"id"`
	UserName   string           `json:"userName"`

	Department struct {
		ID   uint   `json:"id"`
		Name string `json:"name"`
	} `json:"department"`

	Job        struct {
		ID   uint   `json:"id"`
		Name string `json:"name"`
	} `json:"job"`

	Sex    bool   `json:"sex"`
	Email  string `json:"email"`
	Phone  string `json:"phone"`
	Enable bool   `json:"enable"`
}

type UserCopy struct {
	User
}

func (u *UserCopy) MarshalJSON() ([]byte, error) {
	user := CustomUser{
		ID:       u.ID,
		UserName: u.UserName,
		Sex:      u.Sex,
		Email:    u.Email,
		Phone:    u.Phone,
		Enable:   u.Enable,
	}
	user.Department.Name = u.Department.Name
	user.Department.ID = u.Department.ID
	user.Job.ID = u.Job.ID
	user.Job.Name = u.Job.Name
	return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(user)
}